/*******************************************************************************
* Purpose: Assemble control variables used in the analysis
* 
* Dependencies:
*   Input: DSC0001.dta, Primary Analysis Dataset
*		   DSC0007.dta, Birth Registration Part A
		   DSC0009.dta, Birth Registration Part B
*		   DSC0015.dta, 40-week Family Interview
*		   DSC0021.dta, 4-Months Family Interview
		   DSC0035.dta, 12-moths Family Interview
*		   DSC0059.dta, 18-Month PPVT-R Summary
*   Output: controls.dta
*
* Notes: -
*******************************************************************************/
set more off
clear all

global storage: env storage

use "$storage/ece_parenting/raw_data/DSC0001.dta"
sort IHDP

// F3A, Birth registration Part A, DS0144 pdf p. 289
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0007.dta", generate(_mergeF3A)
// F3B, Birth registration Part B, DS0144 pdf p.297  
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0009.dta", generate(_mergeF3B)
// F7, 40-Week Family Interview, DS0144 pdf p.313  
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0015.dta", generate(_mergeF7)
// F11, 4-Month Family Interview, DS0144 pdf p.359
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0021.dta", generate(_mergeF11)
// F18, 12-Month Family Interview, DS0144 pdf p. 435
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0035.dta", generate(_mergeF18)
// F33, 18-Month PPVT-R Summary, DS0144, p.591
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0059.dta", generate(_mergeF33)

// keep primary sample
drop if PAG == 0

// recode treatment variable
replace TG = "1" if TG == "I"
replace TG = "0" if TG == "F"
destring TG, replace 

// create twin indicatior
gen TWIN = . 
replace TWIN = 1 if TWINIHDP != 0
replace TWIN = 0 if TWIN == .
lab var TWIN "Twin Indicator"

// recode cousin pairs
replace TWIN = 0 if IHDP == 5143 | IHDP == 5254 | IHDP == 5163 | IHDP == 5429

// treatment-twin interaction
gen TWIN_TG = TWIN * TG
lab var TWIN_TG "Twin*Treatment"

// recode SEX variable
replace SEX = "1" if SEX == "M"
replace SEX = "0" if SEX == "F"
destring SEX, replace 

// create dummy for Black Mother/Child
gen BLACK = 1 if F3BV60 == 2
replace BLACK = 0 if BLACK == .
lab var BLACK "Black (by Mother)"

// create dummy for Black Mother/Child
gen HISPANIC = 1 if F3BV60 == 3
replace HISPANIC = 0 if HISPANIC == .
lab var HISPANIC "Hispanic (by Mother)"

// create dummy for marital status
gen MARRIED = 1 if F3BV64 == 1
replace MARRIED = 0 if MARRIED == .
lab var MARRIED "Mother Married Indicator"

// create dummy for being in welfare
gen WELFARE = 1 if V60d == 1
replace WELFARE = 0 if V60d == 2
lab var WELFARE "Welfare Indicator (at 12 month)"

// employment status
gen WORKS = .
replace WORKS = 1 if V23g1 == 1 | (V23ilm >= 5 & V23ily == 85)
replace WORKS = 0 if WORKS == .
lab var WORKS "Mother Employed"

// maternal education
rename V49d1 MEDUC
lab var MEDUC "Highest grade completed (Mother)"

// recode if program designated head of household (see DS0144, p.73) 
replace V22f = V22f - 20 if V22f >= 20

// older than 63 y.o. dependents at birth
forvalues x = 1/10 {
   gen old`x'= 0
   replace old`x'= 1 if V23c`x'> 63 & V23g`x' == 2 
}
egen dep_old = rowtotal(old*)
replace dep_old = . if V23b1 == .
drop old*
lab var dep_old "Dependents Age > 63"

// younger than 18 dependents at birth 
forvalues x = 1/10 {
   gen young`x'= 0
   replace young`x'= 1 if V23c`x'< 18 & V23g`x' == 2 
}
egen dep_young = rowtotal(young*)
replace dep_young = . if V23b1 == .
drop young*
lab var dep_young "Dependents Age < 18"

// employed adults in household at birth
forvalues x = 1/10 {
   gen adult`x'= .
   replace adult`x'= 1 if V23c`x'<= 63 & V23c`x'>= 18 & V23g`x' == 1 
}
egen employed_adult = rowtotal(adult*)
replace employed_adult = . if V23b1 == .
drop adult*
lab var employed_adult "Employed Adults in HH"

// siblings at birth 
forvalues x = 1/10 {
   gen sibling`x'= .
   replace sibling`x'= 1 if V23a`x'== 3 | V23a`x' == 4 
}
egen tot_siblings = rowtotal(sibling*), missing
* subtract the infant if HH reports more than 0 children
replace tot_siblings = tot_siblings - 1 if tot_siblings > 0 & TWIN == 0
replace tot_siblings = tot_siblings - 2 if tot_siblings > 0 & TWIN == 1
* assume no siblings other than the twin
replace tot_sibling = 0 if tot_sibling < 0
drop sibling*
lab var tot_siblings "Number of Siblings"

// natural siblings at birth 
forvalues x = 1/10 {
   gen sibling`x'= .
   replace sibling`x'= 1 if V23a`x'== 3 
}
egen tot_siblings_natural = rowtotal(sibling*), missing
replace tot_siblings_natural = tot_siblings_natural - 1 if tot_siblings_natural > 0 & TWIN == 0
replace tot_siblings_natural = tot_siblings_natural - 2 if tot_siblings_natural > 0 & TWIN == 1
replace tot_siblings_natural = 0 if tot_siblings_natural < 0
drop sibling*
lab var tot_siblings_natural "Number of Natural Siblings"

// adult dependents at birth 
forvalues x = 1/10 {
   gen adult`x'= .
   replace adult`x'= 1 if V23c`x' >= 18 & V23c`x' <= 63 & (V23g`x' == 2 | V23g`x' == .)
}
egen dep_adult = rowtotal(adult*)
replace dep_adult = . if V23b1 == .
drop adult*
lab var dep_adult "Dependent 18 < Age <= 63"

// government expenditure per capita
merge m:1 SITE using "$storage/ece_parenting/intermediate_data/us_1985/gpc.dta", nogenerate
lab var GPC "Government Expenditure per Capita (1985)"

// employment rate
merge m:1 SITE using "$storage/ece_parenting/intermediate_data/us_1985/employment.dta", nogenerate
lab var EMPLOYMENT "Employment Rate in the State (1985)"

// median income (in 1000s)
merge m:1 SITE using "$storage/ece_parenting/intermediate_data/us_1985/medinc.dta", nogenerate
lab var MEDINC "Median Income in the State (1985)"

keep IHDP BW ANGA TWINIHDP BWG MEDUC SITE PAG MAGE TG TWIN TWIN_TG SEX BLACK ///
	 HISPANIC MARRIED WELFARE WORKS dep_old dep_young employed_adult ///
	 tot_siblings tot_siblings_natural maternal_PPVT_R dep_adult GPC ///
	 EMPLOYMENT MEDINC BWG

sort IHDP

save "$storage/ece_parenting/intermediate_data/controls.dta", replace
